//===================================================================================
// $$
//
// Coder: Code Milker v1.0
// 0 - EntityName
// 1 - lower
//===================================================================================


namespace App.Data.SqlServer.Repositories {
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using App.Model;

    public class TagRepository : BaseRepository, ITagRepository {
        public TagRepository(IUnitOfWork unitOfWork)
            : base(unitOfWork) {
        }
        public void Create(Tag tag) {
            this.GetDbSet<Tag>().Add(tag);
            this.UnitOfWork.SaveChanges();
        }

        public Tag GetTag(Guid tagId) {
            return this.GetDbSet<Tag>()
                .Where(c => c.Id == tagId)
                .Single();
        }

        public void Update(Tag updatedTag) {
            this.GetDbSet<Tag>().Attach(updatedTag);
            this.SetEntityState(updatedTag, updatedTag.Id == Guid.Empty
                ? EntityState.Added
                : EntityState.Modified);
            this.UnitOfWork.SaveChanges();
        }

        public void Delete(Guid tagId) {
            Tag TagToDelete = this.GetTag(tagId);
            this.GetDbSet<Tag>().Remove(TagToDelete);
            this.UnitOfWork.SaveChanges();
        }

        public IQueryable<Tag> Find()
        {
            return this.GetDbSet<Tag>();
        }

        public IQueryable<Tag> GetAll()
        {
            return this.GetDbSet<Tag>();
        }
    }
}
